﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace WSPDocumentBuilder_WPF.Managers
{
    public static class XmlManager
    {
        public static void MergeTo(string[] xmlFilePaths, string mergedXmlFilePath, Action<XDocument, string> actionOnRootElelement)
        {
            // Création du document Xml de retour
            XDocument resultDoc = new XDocument();
            resultDoc.Add(new XElement("Merged"));

            // Ajout des racines des différents documents
            foreach (string xmlFilePath in xmlFilePaths)
            {
                XDocument xBuffer = XDocument.Load(xmlFilePath);

                // modification si nécessaire
                if (actionOnRootElelement != null)
                {
                    actionOnRootElelement(xBuffer, System.IO.Path.GetFileNameWithoutExtension(xmlFilePath));
                }

                resultDoc.Root.Add(xBuffer.Root);
            }

            // Enregistrement
            resultDoc.Save(mergedXmlFilePath);
        }
    }
}
